package com.wing.purchase.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.wing.common.utils.PageResult;

import com.wing.purchase.model.response.PurchaseDetailExportVO;
import com.wing.purchase.model.response.PurchaseDetailStaticExportVO;
import com.wing.purchase.model.response.PurchaseProductInfoVO;
import com.wing.purchase.service.PurchaseStorageWorkService;

import com.wing.utils.ExcelUtils;
import com.wing.web.db.config.util.DynamicDataSource;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 采购入库作业详情
 *
 * @author
 */
@Validated
@Api(value = "采购入库作业详情管理", tags = "#/purchase/purchaseStorageWorkDetail")
@RestController("PurchaseStorageWorkInfoController")
@RequestMapping("purchase/admin/purchaseStorageWorkDetail")
@RequiredArgsConstructor
public class PurchaseStorageWorkInfoController {

    private final PurchaseStorageWorkService purchaseStorageWorkService;


    @ApiOperation(value = "查询所有采购入库作业详情", notes = "搜索")
    @GetMapping("/list")
    public PageResult<PurchaseProductInfoVO> list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit, @RequestParam(defaultValue = "create_time") String sort, @RequestParam(defaultValue = "desc") String order,
                                             String workNo, String skuName, String skuCode,String warehouseShelfSpaceCode,String warehouseCode,String warehouseAreaId,String warehouseId) {
        Page<PurchaseProductInfoVO> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();
        // 搜索
        if (StringUtils.isNotBlank(workNo)) {
            params.put("workNo", workNo);
        }
        if (StringUtils.isNotBlank(skuName)) {
            params.put("skuName", skuName);
        }
        if (StringUtils.isNotBlank(skuCode)) {
            params.put("skuCode", skuCode);
        }
        if (StringUtils.isNotBlank(warehouseShelfSpaceCode)) {
            params.put("warehouseShelfSpaceCode", warehouseShelfSpaceCode);
        }
        if (StringUtils.isNotBlank(warehouseCode)) {
            params.put("warehouseCode", warehouseCode);
        }
        if (StringUtils.isNotBlank(warehouseAreaId)) {
            params.put("warehouseName", warehouseAreaId);
        }
        if (StringUtils.isNotBlank(warehouseId)) {
            params.put("warehouseId", warehouseId);
        }
        Page<PurchaseProductInfoVO> list = purchaseStorageWorkService.getInProductInfoDetail(pg, params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "导出")
    @GetMapping("/export")
    public void export(HttpServletResponse response,
                       @RequestParam(name = "skuCode", required = false) String skuCode,
                       @RequestParam(name = "skuName", required = false) String skuName,
                       @RequestParam(name = "workNo", required = false) String workNo,
                       @RequestParam(name = "warehouseShelfSpaceCode", required = false) String warehouseShelfSpaceCode,
                       @RequestParam(name = "warehouseCode", required = false) String warehouseCode,
                       @RequestParam(name = "warehouseAreaId", required = false) String warehouseAreaId,
                       @RequestParam(name = "warehouseId", required = false) String warehouseId
    ){
        List<PurchaseDetailExportVO> list = new ArrayList<>();

        Map params = new HashMap();
        if(StringUtils.isNotBlank(skuCode)){
            params.put("skuCode", skuCode);
        }
        if(StringUtils.isNotBlank(skuName)){
            params.put("skuName", skuName);
        }

        if(StringUtils.isNotBlank(workNo)){
            params.put("workNo", workNo);
        }

        if(StringUtils.isNotBlank(warehouseShelfSpaceCode)){
            params.put("warehouseShelfSpaceCode", warehouseShelfSpaceCode);
        }
        if(StringUtils.isNotBlank(warehouseCode)){
            params.put("warehouseCode", warehouseCode);
        }
        if(StringUtils.isNotBlank(warehouseAreaId)){
            params.put("warehouseAreaId", warehouseAreaId);
        }
        if (StringUtils.isNotBlank(warehouseId)) {
            params.put("warehouseId", warehouseId);
        }
        String databaseUser = DynamicDataSource.USER_PREFIX;
        if (StringUtils.isNotBlank(DynamicDataSource.name.get())) {
            databaseUser = databaseUser + "-" + DynamicDataSource.name.get();
        }
        params.put("databaseUser", databaseUser);

        list = purchaseStorageWorkService.exportInProductInfoDetail(params);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
        String timestamp = sdf.format(new Date());
        // 生成带时间戳的文件名
        String fileName = "入库详细表_" + timestamp;
        ExcelUtils.export(response,fileName,list, PurchaseDetailExportVO.class);
    }
}
